﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace SMSSendReceiveTool
{
    public partial class Form1 : Form
    {
        private List<Message> _messageList;
        private List<Message> _tobeList;
        private List<Message> _backupList;
        private bool _running;
        private int _rand;
        Thread bingoThread;

        /// <summary>
        /// 初始化年会相关数据
        /// </summary>
        private void MeetingPrepare()
        {
            _messageList = new List<Message>();
            _tobeList = new List<Message>();
            _running = true;
        }

        /// <summary>
        /// 进入年会相关流程处理
        /// </summary>
        /// <param name="mobileNo">发来短信的手机号</param>
        /// <param name="content">发来短信的内容</param>
        /// <param name="time">发来短信的时间</param>
        private void MeetingProcess(string mobileNo, string content, string time)
        {
            //  处理一下号码
            string nm = mobileNo;
            if (mobileNo.Length >= 3)
            {
                if (mobileNo.Substring(0, 2) == "86")
                    nm = mobileNo.Substring(2);
                else if (mobileNo.Substring(0, 2) == "+86")
                    nm = mobileNo.Substring(3);
            }

            //  处理未知编码格式的内容
            string ct = content;
            if (ct == "未知编码格式")
                ct = "衷心祝愿飞信越做越好! 耶!";

            //  保存信息
            _messageList.Add(new Message(nm, ct, time));

            //  处理显示
            ShowScroll();
        }

        private void ShowScroll()
        {
            List<Label> labelShows = new List<Label>();
            labelShows.Add(labelShow1);
            labelShows.Add(labelShow2);
            labelShows.Add(labelShow3);
            labelShows.Add(labelShow4);
            labelShows.Add(labelShow5);
            labelShows.Add(labelShow6);
            labelShows.Add(labelShow7);

            for (int i = 6; i >= 0; i--)
            {
                if (i != 0)
                {
                    labelShows[i].Text = labelShows[i - 1].Text;
                }
                else
                {
                    Message message = _messageList[_messageList.Count - 1];
                    labelShows[i].Text = string.Format("{0}:\n{1}\n- {2}", message.MobileNo, message.Content, message.Time);
                }
            }
        }

        private void UIFix()
        {
            labelShow1.Size = new System.Drawing.Size(labelShow1.Parent.ClientSize.Width - 20, labelShow1.Parent.ClientSize.Height / 2 - 50);

            labelShow2.Location = new System.Drawing.Point(labelShow1.Location.X, labelShow1.Location.Y + labelShow1.Size.Height + 10);
            labelShow2.Size = new System.Drawing.Size((labelShow1.Parent.ClientSize.Width - 50) / 3, labelShow1.Size.Height / 2);

            labelShow3.Location = new System.Drawing.Point(labelShow2.Location.X + labelShow2.Size.Width + 10, labelShow2.Location.Y);
            labelShow3.Size = labelShow2.Size;

            labelShow4.Location = new System.Drawing.Point(labelShow3.Location.X + labelShow3.Size.Width + 10, labelShow3.Location.Y);
            labelShow4.Size = labelShow3.Size;

            labelShow5.Location = new System.Drawing.Point(labelShow2.Location.X, labelShow2.Location.Y + labelShow2.Size.Height + 10);
            labelShow5.Size = labelShow2.Size;

            labelShow6.Location = new System.Drawing.Point(labelShow3.Location.X, labelShow5.Location.Y);
            labelShow6.Size = labelShow5.Size;

            labelShow7.Location = new System.Drawing.Point(labelShow4.Location.X, labelShow6.Location.Y);
            labelShow7.Size = labelShow6.Size;

            button12.Location = new System.Drawing.Point(labelShow1.Location.X, labelShow5.Location.Y + labelShow5.Size.Height + 10);
            button13.Location = new System.Drawing.Point(labelShow3.Location.X, button12.Location.Y);
            button14.Location = new System.Drawing.Point(labelShow4.Location.X, button13.Location.Y);
            button15.Location = new System.Drawing.Point(button14.Location.X + button14.Size.Width + 5, button14.Location.Y);
            button17.Location = new System.Drawing.Point(button12.Location.X, button12.Location.Y + button12.Size.Height + 5);
        }

        /// <summary>
        /// 准备待抽奖列表
        /// </summary>
        private void PrepareTobeList()
        {
            foreach (Message m in _messageList)
            {
                string no = m.MobileNo;
                bool isDuplicated = false;
                foreach (Message mm in _tobeList)
                {
                    if (mm.MobileNo == no)
                    {
                        isDuplicated = true;
                        break;
                    }
                }
                if (!isDuplicated)
                    _tobeList.Add(m);
            }
            _backupList = _tobeList;
        }

        private delegate void UpdateUIDelegate(int index, string content);

        /// <summary>
        /// 抽奖
        /// </summary>
        private void Bingo()
        {
            Random ra = new Random();
            _running = true;
            List<Label> labelShows = new List<Label>();
            labelShows.Add(labelShow1);
            labelShows.Add(labelShow2);
            labelShows.Add(labelShow3);
            labelShows.Add(labelShow4);
            labelShows.Add(labelShow5);
            labelShows.Add(labelShow6);
            labelShows.Add(labelShow7);

            while (_running)
            {
                _rand = ra.Next(_tobeList.Count);

                for (int i = 6; i >= 0; i--)
                {
                    if (i != 0)
                    {
                        //labelShows[i].Text = labelShows[i - 1].Text;
                        labelShows[i].BeginInvoke(new UpdateUIDelegate(UpdateUI), new object[] {i, string.Empty});
                    }
                    else
                    {
                        Message message = _tobeList[_rand];
                        //labelShows[i].Text = string.Format("{0}:\n{1}\n- {2}", message.MobileNo, message.Content, message.Time);
                        string str = string.Format("{0}:\n{1}\n- {2}", message.MobileNo, message.Content, message.Time);
                        labelShows[i].BeginInvoke(new UpdateUIDelegate(UpdateUI), new object[] {i, str});
                    }
                    Thread.Sleep(10);
                }

            }
        }

        private void UpdateUI(int i, string content)
        {
            List<Label> labelShows = new List<Label>();
            labelShows.Add(labelShow1);
            labelShows.Add(labelShow2);
            labelShows.Add(labelShow3);
            labelShows.Add(labelShow4);
            labelShows.Add(labelShow5);
            labelShows.Add(labelShow6);
            labelShows.Add(labelShow7);

            if (content == string.Empty)
                labelShows[i].Text = labelShows[i - 1].Text;
            else
            {
                labelShows[i].Text = content;
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            MeetingProcess("13811050740", "这原本是从北京飞往沿海城市福州的一次常规飞行。航空公司是一家新成立的国有公司，飞机也是新进口的。但乘务组兴奋有余、专业不足，让我觉得很一般。", "");
            MeetingProcess("2", "终于要降落了。脚下郁郁葱葱的绿色大地和星罗密布的农舍和猪圈，离我们越来越近。", "");
            MeetingProcess("8613811050740", "Factor is a general purpose, dynamically typed, stack-based programming language. ", "");
            MeetingProcess("4", "这篇所说的是关于建立python调用Flash的本地应用，不同于Adobe的Apollo。", "");
            MeetingProcess("5", "为推动中国互联网产业的发展，提升中国网站的服务质量和用户体验，《互联网周刊》与全球知名的网络服务机构gomez共同推出", "");
            MeetingProcess("6", "笔者最近在给编辑部培训的时候发现一个问题，就是他们在设计专题或者页面的时候自我意识太强，缺少页面设计的基本思维。", "");
            MeetingProcess("7", "1月16日，第二届中国IDC产业年度大典在北京拉开序幕，本届年度大典将话题聚焦在“构建绿色IDC产业环境”和“互联网增值服务创新”。", "");
            MeetingProcess("8", "未知编码格式", "");
            MeetingProcess("9", "1月9日，中国年度技术创新大会在北京举行。本次会议由中国计算机用户协会主办，IT专家网和中国互联网技术精英俱乐部承办。", "");
        }

        private void button13_Click(object sender, EventArgs e)
        {
            UIFix();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            //  准备待抽奖列表
            if (_tobeList.Count == 0)
                PrepareTobeList();

            //  开始抽奖
            bingoThread = new Thread(new ThreadStart(Bingo));
            bingoThread.Start();
            //Bingo();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            //_running = false;
            bingoThread.Abort();

            string no = labelShow1.Text.Substring(0, labelShow1.Text.IndexOf(':'));
            for (int i = 0; i < _tobeList.Count; i++)
            {
                if (_tobeList[i].MobileNo == no)
                {
                    textBox1.Text = no + " | " + _tobeList[i].MobileNo;
                    _tobeList.RemoveAt(i);
                    break;
                }
            }
        }

        /// <summary>
        /// 额外添加号码的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button16_Click(object sender, EventArgs e)
        {
            string number = textBox2.Text.Trim();
            string content = textBox4.Text.Trim();
            if (number.Length == 0)
                return;

            MeetingProcess(number, content, DateTime.Now.ToString());
        }

        /// <summary>
        /// 复位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button17_Click(object sender, EventArgs e)
        {
            _tobeList = _backupList;
            MessageBox.Show("Reset Done!");
        }

    }

    /// <summary>
    /// 消息类
    /// </summary>
    public class Message
    {
        public string MobileNo; //  
        public string Content;  //  
        public string Time;     //  

        public Message(string mobileNo, string content, string time)
        {
            this.MobileNo = mobileNo;
            this.Content = content;
            this.Time = time;
        }
    }
}
